﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.Alias"/>.
    /// </summary>
    public class AliasVM : StatementVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(Alias),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new AliasVM((Alias)codeObject, dictionary); });
        }

        #endregion

        #region /* FIELDS */

        protected ExpressionVM _expressionVM;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.Alias"/>.
        /// </summary>
        public AliasVM(Alias alias, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(alias, dictionary)
        {
            _expressionVM = (ExpressionVM)CreateVM(alias.Expression, false, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.Alias"/> model.
        /// </summary>
        public Alias Alias
        {
            get { return (Alias)CodeObject; }
        }

        /// <summary>
        /// The aliased <see cref="Expression"/>.
        /// </summary>
        public ExpressionVM Expression
        {
            get { return _expressionVM; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public override Brush BorderBrush
        {
            get { return Brushes.Pink; }
        }

        public override Brush BackgroundBrush
        {
            get { return Brushes.MistyRose; }
        }

        protected override void RenderArgument(CodeRenderer renderer, RenderFlags flags)
        {
            Expression expression = _expressionVM.Expression.SkipPrefixes();
            Brush brush;
            if (expression is NamespaceRef)
                brush = IDENTIFIER_BRUSH;
            else if (expression is TypeRef)
                brush = TYPE_BRUSH;
            else
                brush = ERROR_BRUSH;

            renderer.RenderText(Alias.Name, brush, this);
            renderer.RenderText(" ", PUNC_BRUSH, this);
            if (OperatorVM.UseAlternativeDisplay)
                AssignmentVM.AltRenderOperator(renderer, OPERATOR_BRUSH, this);
            else
                renderer.RenderText(Alias.ParseTokenSeparator, OPERATOR_BRUSH, this);
            if (_expressionVM != null)
                _expressionVM.Render(renderer, flags | RenderFlags.PrefixSpace);
        }

        public override void UnRender()
        {
            _expressionVM.UnRender();
            base.UnRender();
        }

        public override void RenderToolTip(CodeRenderer renderer, RenderFlags flags)
        {
            TypeRefBaseVM.RenderType(renderer, CodeObject.GetType(), flags, this);
            renderer.RenderText(" :  ", NORMAL_BRUSH, TextStyle.Proportional | TextStyle.Bold);
            RenderDescription(renderer);

            Expression expression = _expressionVM.Expression.SkipPrefixes();
            if (expression is NamespaceRef)
                renderer.RenderText(" (namespace alias)", NORMAL_BRUSH, TextStyle.Proportional);
            else if (expression is TypeRef)
                renderer.RenderText(" (type alias)", NORMAL_BRUSH, TextStyle.Proportional);
            else
                renderer.RenderText(" (unknown if namespace alias or type alias)", NORMAL_BRUSH, TextStyle.Proportional);

            RenderSummaryCommentInToolTip(renderer);
            RenderMessagesInToolTip(renderer, flags);
        }

        #endregion
    }
}
